package com.ruoyi.system.service.impl;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.mapper.CompanyMapper;
import com.ruoyi.system.mapper.LeftoverInMapper;
import com.ruoyi.system.mapper.LeftoverOutMapper;
import com.ruoyi.system.service.ILeftoverOutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class LeftoverOutServiceImpl implements ILeftoverOutService {

    @Autowired
    private LeftoverOutMapper leftoverOutMapper;
    @Autowired
    private LeftoverInMapper leftoverInMapper;
    @Autowired
    private CompanyMapper companyMapper;

    @Override
//    @DataSource(value = DataSourceType.SLAVE)
    public List<LeftoverOutDetail> selectLeftoverOutList(LeftoverOutDetail leftoverOutDetail) {
        return leftoverOutMapper.selectLeftoverOutList(leftoverOutDetail);
    }

    @Override
    public LeftoverOutDetail selectLeftoverOutByTableId(String tableId) {
        return leftoverOutMapper.selectLeftoverByTableId(tableId);
    }

    @Override
    @Transactional
    public int updateLeftoverOutDetail(LeftoverOutDetail leftoverOutDetail) {
        return leftoverOutMapper.updateLeftover(leftoverOutDetail);
    }

    @Override
    public int insertLeftoverOut(LeftoverOutMessage leftoverOutMessage, LeftoverOutDetail[] leftoverOutDetails) throws IOException {

        String outId = UUID.randomUUID().toString();
        LeftoverOut leftoverOut = new LeftoverOut(leftoverOutMessage);
        leftoverOut.setOutId(outId);
        leftoverOutMapper.insertLeftoverOut(leftoverOut);

        for (LeftoverOutDetail leftoverOutDetail : leftoverOutDetails) {
            leftoverOutDetail.setTableId(UUID.randomUUID().toString());
            leftoverOutDetail.setOutId(outId);
            leftoverOutDetail.setUnit(leftoverOutMessage.getWeighUnit());
            leftoverOutMapper.insertLeftoverOutDetail(leftoverOutDetail);
        }
        return 1;
    }

    @Override
    public int updateLeftoverOutState(LeftoverOut leftoverOut) {
        String tableId = leftoverOut.getTableId();
        LeftoverOutDetail leftoverOutDetail = leftoverOutMapper.selectLeftoverOutDetailByTableId(tableId);
        String companyId = leftoverOutDetail.getCompanyId();
        Double leftoverOutNum = leftoverOutDetail.getLeftoverOutNum();
        companyMapper.updateNum(companyId, leftoverOutNum);
        return leftoverOutMapper.updateLeftoverOutState(leftoverOut);
    }

    @Override
    public List<Series> handelData() {
        List<String> materials = leftoverInMapper.selectMaterial();
        List<Series> seriesList = new ArrayList<>();
        for (String material : materials) {
            Series series = new Series();
            series.setName(material);
            series.setType("bar");
            series.setBarGap(0);
            int[] data;
            data = selectData(material);
            series.setData(data);
            seriesList.add(series);
        }
        return seriesList;
    }

    @Override
    public List<OutChart> selectLeftoverOutChart(LeftoverOutDetail leftoverOutDetail) {
        return leftoverOutMapper.selectLeftoverOutChart(leftoverOutDetail);
    }

    private int[] selectData(String material) {
        return leftoverOutMapper.selectData(material);
    }
}
